<?php
/**
 * Controller to manage categories assigned to product in admin area
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @package Products Module
 * @license  New BSD License
 * 
 */
class admin_ProductAttributesController extends Bel_Controller_Aed {
	
	/**
	 * Initialization
	 *
	 */
	function init() {
		$this->_model = new Models_ProductAttributes( );
		$this->_formname = 'product_attributes_form';
		$this->_items_on_page = 25;
		parent::init ();	
		$this->_form_template = 'products/attributes/form.tpl';	
		$this->_grid_template = 'products/attributes/grid.tpl';
	}
	
	/**
	 * Deletes attribute's value. Is called via AJAX request
	 *
	 */
	public function deletevalueAction(){
		$defaultValues = new Models_ProductAttributes_DefaultValues();
		$defaultValues->deleteEntryById($this->_request->getParam('value_id'));
		
		echo Zend_Json::encode(array('value_id'=>$this->_request->getParam('value_id')));
		die;
	}
	
	/**
	 * Populates field types dropdown with values
	 *
	 * @param Bel_Forms_Builder $form
	 */	
	protected function _prepareForm(Bel_Forms_Builder $form){
		$options = array ();
		$fields = array ('text','password','checkbox','radio','submit','reset','hidden','textarea','image','button','select','file');
		foreach ( $fields as $field ) {
			$options [$field] = $field;
		}		
		$form->addSubElement ( 'attribute_field_type', 'options', $options );
		
		if($this->_current_entry){
			$this->view->assign('exist_values',$this->_current_entry['default_values']);
		}		
	}
	
	/**
	 * Adds default values for attribute. After editing clears products cache
	 *
	 */	
	protected function _afterModify(){		
		if($this->_added_entry){
			$this->_addValues($this->_added_entry['value']);
		}elseif($this->_request->getParam('attribute_id')){			
			$this->_addValues($this->_request->getParam('attribute_id'));			
			Helpers_Products_Cache::cleanProductCacheByAttribute($this->_request->getParam('attribute_id'));
		}
	}
	
	/**
	 * Clears products cache before attribute deletion
	 *
	 */
	protected function _beforeDelete(){
		Helpers_Products_Cache::cleanProductCacheByAttribute($this->_request->getParam('attribute_id'));
	}
	
	/**
	 * Add default values to attribute
	 *
	 * @param integer $attribute_id
	 */
	private function _addValues($attribute_id){
		
		$field_type = $this->_request->getParam('attribute_field_type');
		if($field_type=='radio' || $field_type=='select'){
			$labels = $this->_request->getParam('value_text');
			$values = $this->_request->getParam('value_value');
			$value_id = $this->_request->getParam('value_id');
			
			if($labels && $values){
				$oAttrVal = new Models_ProductAttributes_DefaultValues();
				
				if($value_id){
					$oAttrVal->bulkDeleteById($value_id);
				}
				
				for($i=0; $i<count($labels); $i++){
					if($labels[$i] && $values[$i]){
						$oAttrVal->addEntry(array('attribute_id'=>$attribute_id,'value_text'=>$labels[$i],'value_value'=>$values[$i]));
					}
				}
			}
		}else{
			$oAttrVal = new Models_ProductAttributes_DefaultValues();
			$oAttrVal->deleteValuesByAttribute($attribute_id);
		}
	}	
}